import json
from utils.getDataFromMysql import *
from utils.expand import *

@timecost
def generateTable(result_list: list) -> str:
    json_result = {
        'columns': [],
        'tableData': []
    }

    if not result_list:
        return json.dumps(json_result)

    attributes = list(result_list[0].__dict__.keys())
    json_result['columns'] = [{'prop': attr, 'label': attr} for attr in attributes]
    json_result['tableData'] = [result.__dict__ for result in result_list]

    return json.dumps(json_result)

if __name__ == '__main__':
    sql_query = "select * from weo_data limit 10"
    result_list = getDataBySql(sql_query)
    response = generateTable(result_list)
    print(response)